package com.ihighest.app.infox.im;

import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.util.AndroidEmoji;
import io.rong.imkit.widget.provider.IContainerItemProvider.MessageProvider;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ImageListviewAdapter;
import com.ihighest.app.infox.myview.HorizontialListView;

@ProviderTag(messageContent = IssueMessage.class, showPortrait = false, centerInHorizontal = true)
public class IssueMessageItemProvider extends MessageProvider<IssueMessage> {
	MediaPlayer mp;

	static class ViewHolder {
		TextView tv_text, tv_area, tv_crop, tv_weather, tv_manure, tv_gold;
		ImageView iv_voice;
		HorizontialListView hlv_images;
		LinearLayout message_layout;
	}

	@Override
	public View newView(Context context, ViewGroup group) {
		View view = LayoutInflater.from(context).inflate(R.layout.infox_item_issue_message, null);
		ViewHolder holder = new ViewHolder();
		holder.tv_text = (TextView) view.findViewById(R.id.tv_text);
		holder.tv_area = (TextView) view.findViewById(R.id.tv_area);
		holder.tv_crop = (TextView) view.findViewById(R.id.tv_crop);
		holder.tv_weather = (TextView) view.findViewById(R.id.tv_weather);
		holder.tv_manure = (TextView) view.findViewById(R.id.tv_manure);
		holder.tv_gold = (TextView) view.findViewById(R.id.tv_gold);
		holder.iv_voice = (ImageView) view.findViewById(R.id.iv_voice);
		holder.hlv_images = (HorizontialListView) view.findViewById(R.id.hlv_images);
		holder.message_layout = (LinearLayout) view.findViewById(R.id.message_layout);
		view.setTag(holder);
		return view;
	}

	@Override
	public void bindView(View v, int position, IssueMessage im, UIMessage message) {
		ViewHolder holder = (ViewHolder) v.getTag();
		holder.message_layout.setBackgroundResource(R.drawable.infox_question_green);
		// if (message.getMessageDirection() == Message.MessageDirection.SEND)
		// {// 消息方向，自己发送的
		// holder.message_layout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);
		// } else {
		// holder.message_layout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);
		// }
		if (TextUtils.isEmpty(im.getText())) {
			holder.tv_text.setVisibility(View.GONE);
		} else {
			holder.tv_text.setVisibility(View.VISIBLE);
			holder.tv_text.setText(im.getText());
			AndroidEmoji.ensure(new SpannableString(holder.tv_text.getText()));
		}
		if (TextUtils.isEmpty(im.getArea())) {
			holder.tv_area.setVisibility(View.GONE);
		} else {
			holder.tv_area.setVisibility(View.VISIBLE);
			holder.tv_area.setText("地区：" + im.getArea());
		}
		if (TextUtils.isEmpty(im.getCrop())) {
			holder.tv_crop.setVisibility(View.GONE);
		} else {
			holder.tv_crop.setVisibility(View.VISIBLE);
			holder.tv_crop.setText("作物：" + im.getCrop());
		}
		if (TextUtils.isEmpty(im.getWeather())) {
			holder.tv_weather.setVisibility(View.GONE);
		} else {
			holder.tv_weather.setVisibility(View.VISIBLE);
			holder.tv_weather.setText("天气：" + im.getWeather());
		}
		if (TextUtils.isEmpty(im.getManure())) {
			holder.tv_manure.setVisibility(View.GONE);
		} else {
			holder.tv_manure.setVisibility(View.VISIBLE);
			holder.tv_manure.setText("药、肥：" + im.getManure());
		}
		if (TextUtils.isEmpty(im.getGold())) {
			holder.tv_gold.setVisibility(View.GONE);
		} else {
			holder.tv_gold.setVisibility(View.VISIBLE);
			holder.tv_gold.setText("悬赏：" + im.getGold() + " 个金币");
		}
		if (TextUtils.isEmpty(im.getVoiceUri())) {
			holder.iv_voice.setVisibility(View.GONE);
		} else {
			holder.iv_voice.setVisibility(View.VISIBLE);
			holder.iv_voice.setTag(R.id.tag_1, im.getVoiceUri());
			holder.iv_voice.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					try {
						if (mp != null && mp.isPlaying()) {
							mp.stop();
							mp.release();
							mp = null;
						}
						mp = new MediaPlayer();
						mp.setDataSource((String) v.getTag(R.id.tag_1));
						mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

						// 通过异步的方式装载媒体资源
						mp.prepareAsync();
						mp.setOnPreparedListener(new OnPreparedListener() {
							@Override
							public void onPrepared(MediaPlayer mp) {
								// 装载完毕回调
								mp.start();
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		if (im.getImageUrls() == null || im.getImageUrls().length == 0) {
			holder.hlv_images.setVisibility(View.GONE);
		} else {
			holder.hlv_images.setVisibility(View.VISIBLE);
			ImageListviewAdapter imageListviewAdapter = new ImageListviewAdapter(v.getContext(), im.getImageUrls());
			holder.hlv_images.setAdapter(imageListviewAdapter);
		}

	}

	@Override
	public Spannable getContentSummary(IssueMessage data) {
		return new SpannableString(data.getText());
	}

	@Override
	public void onItemClick(View view, int position, IssueMessage content, UIMessage message) {

	}

	@Override
	public void onItemLongClick(View view, int position, IssueMessage content, UIMessage message) {

	}

}
